    #!/bin/bash
    set -o errexit

    function fatal(){
        echo "$1"
        exit 1
    }

    # image: f00a393d-a1c8-4e93-a13e-c364a7d0a2a9: jenkins-agent-multiarch-15.4.1@3.0.0

    # bindPassword密码,
    # 请以在 coal主机 中执行以下命令的返回值替换
    # sdc-login adminui json ufdsMaster.bindPassword -f /opt/smartdc/adminui/etc/config.json
    bind_password="60a3b0b1e57a62404f61de7616ccca56"

    # 是否首次运行,第一次运行必须要加入
    [[ "$1" == "--first"]] && first_exec="yes"

    # pkgsrc加速源, 如没有请注释, 必须为http协议，不能使用https协议
    #pkgsrc_base=http://pkgsrc.smartos.top

    # manta加速源, 如没有请注释
    manta_base=https://manta.smartos.top

    # sdc-admin源代码地址, 如用官方源请注释
    git_url=https://gitee.com/smartos-club/sdc-adminui.git

    # sdc-admin源代码构建分支，如用master请注释
    git_branch=dev-jack

    # 源码目录
    data_path=/root/gitrepo

    # 首次运行
    if [ "${first_exec}" == "yes" ];then
        echo "首次运行"
        # 替换pkgsrc源
        if [ "${pkgsrc_base}" != "" ];then
            gsed -i "s*http://pkgsrc.joyent.com*${pkgsrc_base}*" /opt/local/etc/pkgin/repositories.conf
            gsed -i "s*http://pkgsrc.joyent.com*${pkgsrc_base}*" /opt/local/etc/pkg_install.conf
        fi
        echo "install packages..."
        pkgin -y in git python27 sudo coreutils gcc7 gmake || true
    
        mkdir -p ${data_path} && cd ${data_path}
        
        echo " clone sdc-admin src"
        [[ "${git_url}" == "" ]] && git_url=https://github.com/joyent/sdc-adminui.git
        git clone ${git_url}

        cd sdc-adminui
        if [ "${git_branch}" != "" ];then
            echo "build branch is ${git_branch}"
            git checkout ${git_branch}
            echo "export ENGBLD_CHECK_COPYRIGHT_ARGS=\"-q -b ${git_branch}\"" >> ~/.bashrc
        fi 

        # 替换git源
        gsed -i 's*github.com/joyent*gitee.com/smartos-club*g' .gitmodules
        gsed -i 's*github.com/trentm*gitee.com/smartos-club*g' .gitmodules
        gsed -i 's*github.com/davepacheco*gitee.com/smartos-club*g' .gitmodules

        # 替换manta_base
        [[ "${manta_base}" == "" ]] || echo "export NODE_PREBUILT_MANTA_URL=${manta_base}" >> ~/.bashrc

        . ~/.bashrc

        # make dev
        make dev
        #cp etc/config.json.in etc/config.json        
        json -e "ufdsMaster.bindPassword=ufds.bindPassword='${bind_password}';this.datacenters.coal.sapi.version='*'" -f etc/config.json.in > etc/config.json

        # create ssl pem
        openssl req -x509 -nodes -subj '/CN=*' -newkey rsa:4096 -sha256 -days 365 -keyout etc/ssl/default.pem -out etc/ssl/default.pem

        # 将node加入PATH
        echo "export PATH=$PATH:${data_path}/sdc-adminui/build/node/bin" >> ~/.bashrc    

    fi


    # 判断是否存在源码目录
    [[ -d "${data_path}/sdc-adminui" ]] || fatal "不存在源码目录，如果是首次运行请执行 ${0} --first"

    # export参数
    . ~/.bashrc

    # 转到源目录
    cd ${data_path}/sdc-adminui
    
    make check
    make devrun